home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12205 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: castle.nando.net!news
  2. From: Caius Martius <caius@nando.net>
  3. Newsgroups: gnu.g++.help,comp.lang.c++
  4. Subject: Re: Why not?  c++ Array of strings...
  5. Date: Mon, 18 Mar 1996 16:10:11 -0800
  6. Organization: Nando.net
  7. Message-ID: <314DFB63.581C@nando.net>
  8. References: <4ii3pp$4df@panix.com>
  9. NNTP-Posting-Host: grail509.nando.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I; 16bit)
  14.  
  15. Arthur Cinader Jr wrote:
  16. > Note Followup
  17. > I want a class to a have a member that is an array of
  18. > character strings that are dynamically allocated.  I have written
  19. > a very simple program that shows the eveolution of my thought
  20. > that leads me to beleieve that I should be able to do what I
  21. > am trying, but I get a segmentation fault and core dump when I
  22. > try it.  Why?  I obviously am missing something.  I have kept
  23. > the code as brief as possible (I'm using g++ v2.4):
  24. > ***foo.h
  25. > class Foo {
  26. >     public:
  27. >         Foo();      // constructor
  28. >         ~Foo();     // destructor
  29. >     private:
  30. >         char a[10];  // declare and define an array of characters
  31. >         char *b;     // declare a string pointer
  32. >         char *c[10]; // declare an array of ten strings?
  33. > };
  34. > ***foo.C
  35. > nclude <iostream.h>
  36. > #include <assert.h>
  37. > #include <string.h>
  38. > #include "foo.h"
  39. > // Constructor -- all the action is here --
  40. > Foo::Foo()
  41. > {
  42. >         // populate the character array
  43. >         a[0] = 'a';
  44. >         a[1] = 'r';
  45. >         a[2] = 't';
  46. >         a[3] = 'h';
  47. >         a[4] = 'u';
  48. >         a[5] = 'r';
  49. >         // print the character array
  50. >         cout << "output from array of char: ";
  51. >         for(int i = 0 ; i < 6 ; i++)
  52. >                 cout << a[i];
  53. >         cout << endl;
  54. >         // populate the string
  55. >         b = new char[7];                // allocate space
  56. >         assert( b != 0);                // make sure space was allocated
  57. >         strcpy(b, "arthur");
  58. >         // print the string
  59. >         cout << "output from *char: " << b;
  60. >         cout << endl;
  61. >         // So far so good.  This is where it all falls apart
  62. >         // populate array of strings
  63. >         c[0] = new char[10];    // allocate space
  64. >         assert(c[0] != 0);      // make sure space was allocated
  65. >         strcpy(c[0], "arthur");
  66. >         c[1] = new char[10];
  67. >         assert(c[1] != 0);
  68. >         strcpy(c[1], "cinader");
  69. >         c[2] = new char[10];
  70. >         assert(c[2] != 0);
  71. >         strcpy(c[2], "jr.");
  72. >         // output array of strings
  73. >         for (i = 0 ; i < 3 ; i--)  // *** This should be unary increment. ++ ***
  74. >                 cout << c[i] << " ";
  75. >         cout << endl;
  76. > }
  77. > Foo::~Foo() {
  78. >         // clean up the mess
  79. >         delete [] b;
  80. >         for(int i = 0; i < 3; i --) // *** This should be unary increment. ++ ***
  81. >                 delete [] c[i];
  82. > }
  83. > **** foo.driver.C
  84. > #include "foo.h"
  85. > main()
  86. > {
  87. >         Foo f;
  88. >         return 0;
  89. > }
  90.